<template>
<el-dialog
    :title="$t('plus.add_gift')"
    v-model="dialogVisible"
    @close="dialogFormVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form size="small" :model="form" :rules="formRules" ref="form">
      <el-form-item
        :label="$t('plus.gift_name')"
        prop="giftName"
        :label-width="formLabelWidth"
        ><el-input v-model="form.giftName" autocomplete="off"></el-input
      ></el-form-item>

      <el-form-item :label="$t('plus.gift_price')" prop="price" :label-width="formLabelWidth">
        <el-input
          type="number"
          v-model.number="form.price"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item
        :label="$t('plus.product_image2')"
        :label-width="formLabelWidth"
        prop="imageId"
      >
        <div class="gift-img-box d-c-c" @click="openProductUpload">
          <div v-if="form.imageId != null && form.imageId > 0">
            <img :src="form.filePath" alt="" />
          </div>
          <div class="gift-img-btn" v-else>
            <el-icon><Plus /></el-icon>
          </div>
        </div>
      </el-form-item>

      <el-form-item :label="$t('plus.is_hot')" :label-width="formLabelWidth">
        <el-radio-group v-model="form.isHot">
          <el-radio :label="1">{{$t('plus.yes')}}</el-radio>
          <el-radio :label="0">{{$t('plus.no')}}</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item :label="$t('plus.is_event')" :label-width="formLabelWidth">
        <el-radio-group v-model="form.isActive">
          <el-radio :label="1">{{$t('plus.yes')}}</el-radio>
          <el-radio :label="0">{{$t('plus.no')}}</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item :label="$t('plus.category_sort')" prop="sort" :label-width="formLabelWidth"
        ><el-input v-model.number="form.sort" autocomplete="off"></el-input
      ></el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible">{{$t('plus.cancel')}}</el-button>
        <el-button type="primary" @click="addFunc" :loading="loading"
          >{{$t('plus.confirm')}}</el-button
        >
      </div>
    </template>
    <!--商品图片组件-->
    <Upload
      v-if="isProductUpload"
      :config="{  total: 1}"
      :isupload="isProductUpload"
      @returnImgs="returnProductImgsFunc"
      >{{$t('plus.upload_image_button')}}</Upload
    >
  </el-dialog>
</template>

<script>
import liveApi from "@/api/live.js";
import Upload from "@/components/file/Upload.vue";
export default {
  components: {
    Upload
  },
  data() {
    return {
      form: {
        name: "",
        price: "",
        isHot: 0,
        isActive: 0,
        sort: 100
      },
      formRules: {
        giftName: [{
          required: true,
          message: $t("plus.input_gift_name"),
          trigger: "blur"
        }],
        price: [{
          required: true,
          message: $t("plus.input_price"),
          trigger: "blur"
        }],
        imageId: [{
          required: true,
          message: $t("plus.upload_image"),
          trigger: "blur"
        }],
        sort: [{
          required: true,
          message: $t("plus.category_sort_required")
        }, {
          type: "number",
          message: $t("plus.category_sort_numeric")
        }]
      },
      /*左边长度*/
      formLabelWidth: "120px",
      /*是否显示*/
      dialogVisible: false,
      loading: false,
      /*是否上传图片*/
      isProductUpload: false
    };
  },
  props: ["open_add", "addform"],
  created() {
    this.dialogVisible = this.open_add;
  },
  methods: {
    /*添加*/
    addFunc() {
      let self = this;
      let params = self.form;
      self.$refs.form.validate(valid => {
        if (valid) {
          self.loading = true;
          liveApi.addGift(params).then(res => {
            self.loading = false;
            ElMessage({
              message: $t("plus.add_success2"),
              type: "success"
            });
            self.dialogFormVisible(true);
          }).catch(error => {
            self.loading = false;
          });
        }
      });
    },
    /*关闭弹窗*/
    dialogFormVisible(e) {
      if (e) {
        this.$emit("close", {
          type: "success",
          openDialog: false
        });
      } else {
        this.$emit("close", {
          type: "error",
          openDialog: false
        });
      }
    },
    /*打开上传图片*/
    openProductUpload: function () {
      this.isProductUpload = true;
    },
    /*上传商品图片*/
    returnProductImgsFunc(e) {
      if (e != null) {
        this.form.filePath = e[0].filePath;
        this.form.imageId = e[0].fileId;
      }
      this.isProductUpload = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.img {
  margin-top: 10px;
}
.gift-img-box {
  width: 40px;
  height: 40px;
  border: 1px solid #cccccc;
  border-radius: 4px;
}
.gift-img-box img {
  width: 40px;
  height: 40px;
}
</style>

